// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
// require("babel-polyfill")
import "babel-polyfill"
import './element-variables.scss'
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
// import 'element-ui/lib/theme-chalk/index.css';
import './bootstrap-scss/bootstrap.scss'

import VeeValidate from 'vee-validate'
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: 'C9dKsbLbMXNg8zVAWruIcrMPRHaxCMw6'
})

// 自定义验证器
import './validate.js'

// 参数转换插件
import paramsConvert from './plugins/paramsConvert'
// 常用工具插件
import Util from './plugins/Util'
// html转化为pdf
import htmlTopdf from './plugins/htmlTopdf'
// 错误处理
import errorPage from './plugins/errorPage'

// 图片懒加载
import VueLazyload from 'vue-lazyload'


// 需要全局以组件实例调用使用的组件
import './plugins/imgViewer'
// import './plugins/login'

Vue.config.productionTip = false
require('./self.css')
Vue.use(VeeValidate)
Vue.use(paramsConvert)
Vue.use(Util)
Vue.use(htmlTopdf)
Vue.use(errorPage)
Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: 'static/404.png',
  loading: 'static/mohutupian.png',
  attempt: 1,
  // the default is ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend']
  listenEvents: [ 'scroll' ]
})

Vue.use(ElementUI)
// 功能未开通提示
Vue.component('promp', (resolve) => { require(['./components/server/Promp'], resolve) })
// 弹框通用组件
Vue.component('tele-modal', (resolve) => { require(['./components/TeleModal'], resolve) })
// 时间轴组件
Vue.component('timeline-item', (resolve) => { require(['./components/TimeLineItem'], resolve) })
Vue.component('time-line', (resolve) => { require(['./components/TimeLine'], resolve) })
// 评论组件
Vue.component('comments', (resolve) => { require(['./components/server/Comments'], resolve) })
// 返回顶部
Vue.component('back-top', (resolve) => { require(['./components/server/BackTop'], resolve) })
// 城市级联
Vue.component('city-cascader', (resolve) => { require(['./components/server/CtiyCascader'], resolve) })
// 富文本编辑器
Vue.component('text-edit', (resolve) => { require(['./components/server/TextEdit'], resolve) })
// 文件上传
Vue.component('file-upload', (resolve) => { require(['./components/server/FileUpload'], resolve) })

window.onbeforeunload = () => {
  // localStorage.setItem('lastestLeaveTime', new Date().getTime())
}

const routeGa = function (to) {
  if (window.gtag) {
    gtag("config", "UA-126457129-1", {
      page_path: to.path,
      page_title: to.name
    });
  }
}

router.beforeEach(function (to, from, next) {
  // 刷新后的
  
  // 发送页面跳转追踪
  routeGa(to)
    if (to.name === 'signin' || to.name === 'signsuccess') {
      next()
      return
    }
    if (to.path === '/') {
      next({
          replace: true,
          name: 'Home'
      })
      return
    }
    // 跳转至上述页面,需要验证用户是否登录
    let user = localStorage.userinfo ? JSON.parse(localStorage.userinfo) : null
    //未登录
    if (!user || new Date().getTime() - user.expires > 24 * 60 * 60 * 1000) {
      store.commit('loginOut')
      if (to.matched.some(record => record.meta.requiresAuth)) { // 需要用户，增跳转登录也
        next({
          replace: true,
          name: 'Home'
        })
        return
      }
    } else {
      store.commit('setUser', user)
    }
    if (from.name && JSON.stringify(to.params).length > 3) {
      Vue.lsput('routerToParams', to.params)
    }
    next()

})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router: router,
  store: store,
  components: { App },
  template: '<App/>'
})
